<?php

/*
 * This file is part of the gedongdong/laravel_rbac_permission.
 *
 * (c) gedongdong <gedongdong2010@163.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

function can()
{
    $currRouteName = \Illuminate\Support\Facades\Route::currentRouteName();

    if (ends_with($currRouteName, '.white')) {
        return true;
    }

    $user = session()->get('user');
    if (\App\Http\Models\Users::ADMIN_YES == $user['administrator']) {
        return true;
    }

    $role_ids = \App\Http\Models\UsersRoles::where('users_id', $user['id'])->pluck('roles_id')->toArray();

    $permission_ids = \App\Http\Models\RolePermission::whereIn('roles_id', $role_ids)->pluck('permission_id')->toArray();

    $check = \App\Http\Models\Permission::whereIn('id', $permission_ids)->where('routes', 'like', "%{$currRouteName}%")->count();
    if ($check > 0) {
        return true;
    }

    return false;
}

if (!function_exists('ends_with')) {
    /**
     * Determine if a given string ends with a given substring.
     *
     * @param string       $haystack
     * @param string|array $needles
     *
     * @return bool
     */
    function ends_with($haystack, $needles)
    {
        return \Illuminate\Support\Str::endsWith($haystack, $needles);
    }
}

if (!function_exists('get_order_no')) {
    /**
     * 得到新订单号
     * @param string $user_id
     * @return string
     */
    function get_order_no($user_id ='')
    {
        return date('YmdHis').$user_id.mt_rand(100000,999999);
    }
}

if (!function_exists('createQrcode')) {
    /**
     * 生成二维码图片/生成带logo的二维码图片
     * @param $data string 二维码内容
     * @param string $saveDir 保存路径
     * @param string $logo 图片logo路径,需要加入到二维码中的logo，false不带logo
     * @return string
     */
    function createQrcode($data,$saveDir = '/images/report/',$qrcodeName='qrcode',$logo = '') {
        require_once app_path().'/Library/Phpqrcode.php';

        $path = public_path().$saveDir;

        $filename = $path.$qrcodeName.'.png';//生成的文件名 文件存放路径
        $errorCorrectionLevel = 'L';//容错级别：L、M、Q、H
        $matrixPointSize = 6;//生成图片大小：1到10
        //生成二维码图片
        QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

        $QR = $filename;//已经生成的原始二维码图

        if ($logo !== false) {
            $logo = public_path().'/web/images/zhy_logo.jpg';
            
            $QR = imagecreatefromstring(file_get_contents($QR));//目标图象连接资源。
            $logo = imagecreatefromstring(file_get_contents($logo));//源图象连接资源。
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 5;//组合之后logo的宽度
            $scale = $logo_width/$logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
            $logo_qr_height = $logo_height/$scale;//logo的宽度缩放比(本身宽度/组合后的宽度)
            $from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
            //重新组合图片并调整大小
            //imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
            $imagePathName = $filename;
            //输出图片
            imagepng($QR, $imagePathName);
        }else{
            $imagePathName =  $filename;
        }

        // return $imagePathName;
        return $saveDir.$qrcodeName.'.png';
    }
}
